Page({
  data: {
    currentDate: Date.now(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [
    ],
    repairItem: [
    ],
    attachment: [
    ],
    mobile: '',
    description: ''
  },
  onLoad({ id }) {
    // 获取房屋列表
    this.getHouseList(),
      this.getRepairItem()
    if (id) {
      this.getRepairDetail(id)
      wx.setNavigationBarTitle({ title: '编辑报修信息' })
    }
  },
  async getRepairDetail(id) {
    // 请求数据接口
    const { code, data } = await wx.http.get('/repair/' + id)
    // 检测接口调用结果
    if (code !== 10000) return wx.utils.toast()
    // 渲染报修信息
    this.setData({
      ...data,
      houseName: data.houseInfo
    })
  },
  // 获取房屋列表
  async getHouseList() {
    const { code, data } = await wx.http.get('/house')
    if (code !== 10000) return wx.utils.toast()
    // 数据渲染
    this.setData({ houseList: data })
  },
  // 获取维修项目
  async getRepairItem() {
    const { code, data } = await wx.http.get('/repairItem')
    if (code !== 10000) return wx.utils.toast()
    // 数据渲染
    this.setData({ repairItem: data })
  },
  // 选择图片后
  afterRead(ev) {
    console.log(ev.detail.file.url);
    wx.uploadFile({
      url: wx.http.baseURL + '/upload',
      name: 'file',
      filePath: ev.detail.file.url,
      header: {
        Authorization: getApp().token
      },
      success: (res) => {
        // ##########################################
        console.log(JSON.parse(res.data).data);
        this.data.attachment.push(JSON.parse(res.data).data)
        this.setData({
          attachment: this.data.attachment
        })
        // ############################################
      }
    })
  },
  verifyHouse() {
    const valid = !!this.data.houseName
    // 验证结果提示
    if (!valid) wx.utils.toast('请选择报修房屋!')
    // 返回验证结果
    return valid
  },
  verifyRepair() {
    const valid = !!this.data.repairItemName
    // 验证结果提示
    if (!valid) wx.utils.toast('请选择维修项目!')
    // 返回验证结果
    return valid
  },
  verifyMobile() {
    // 验证手机号
    const reg = /^1[3-8]\d{9}$/
    const valid = reg.test(this.data.mobile.trim())
    // 验证结果提示
    if (!valid) wx.utils.toast('请填写正确的手机号码!')
    // 返回验证结果
    return valid
  },
  verifyDate() {
    // 验证日期格式
    const reg = /^\d{4}\/\d{2}\/\d{2}$/
    const valid = reg.test(this.data.appointment)
    // 验证结果提示
    if (!valid) wx.utils.toast('请选择预约日期!')
    // 返回验证结果
    return valid
  },
  verifyDescription() {
    // 验证报修项目描述
    const valid = this.data.description.trim() !== ''
    // 验证结果提示
    if (!valid) wx.utils.toast('请填写问题描述!')
    // 返回验证结果
    return valid
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })

  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({ repairLayerVisible: false })
  },
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  selectDate(ev) {
    console.log(ev);
    this.setData({

      appointment: wx.utils.dateFormat(ev.detail),
      dateLayerVisible: false
    })

  },
  // 选择房屋
  selectHouse(ev) {
    console.log(ev);
    this.setData({
      houseName: ev.detail.name,
      houseId: ev.detail.id
    })
  },
  selectRepairItem(ev) {
    console.log(ev);
    this.setData({
      repairItemName: ev.detail.name,
      repairItemId: ev.detail.id
    })
  },

  // 提交报修
  async submitForm() {
    // 表单校验
    if (!this.verifyHouse()) return
    if (!this.verifyRepair()) return
    if (!this.verifyMobile()) return
    if (!this.verifyDate()) return
    if (!this.verifyDescription()) return
    // 解构接口需要的数据
    const { id, houseId, repairItemId, appointment, mobile, description, attachment } = this.data
    // 调接口
    const { code } = await wx.http.post('/repair', {
      id,
      houseId,
      repairItemId,
      appointment,
      mobile,
      description,
      attachment
    })
    // 检测接口请求的结果
    if (code !== 10000) return wx.showToast()
    // 跳转到报修列表页面
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index'
    })
  }
})
